package com.metaera.app.ui.fragments;

import android.os.Bundle;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.metaera.app.controller.core.IPaginationPresenter;
import com.metaera.app.controller.presenter.SearchMoreListPresenter;
import com.metaera.app.controller.view.core.IPaginationView;
import com.metaera.app.data.model.SearchResultResponse;
import com.metaera.app.ui.adapters.SearchResultActivityAdapter;
import com.metaera.app.ui.adapters.SearchResultChoiceTopicAdapter;
import com.metaera.app.ui.adapters.SearchResultColumnAdapter;
import com.metaera.app.ui.adapters.SearchResultFlashNewsAdapter;
import com.metaera.app.ui.adapters.SearchResultNewsAdapter;
import com.metaera.app.ui.core.BaseRequestListFragment;

import java.util.Objects;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.RecyclerView;

public class SearchResultListFragment extends BaseRequestListFragment<SearchResultResponse> {

    public static Fragment newInstance(String key, int orderType, int timeType, int type) {
        Bundle bundle = new Bundle();
        bundle.putString("keyword", key);
        bundle.putInt("type", type);
        bundle.putInt("orderType", orderType);
        bundle.putInt("timeType", timeType);
        SearchResultListFragment fragment = new SearchResultListFragment();
        fragment.setArguments(bundle);
        return fragment;
    }


    private String keyword;
    private int    type, orderType, timeType;


    @Override
    protected BaseQuickAdapter<SearchResultResponse, BaseViewHolder> createAdapter(RecyclerView recyclerView) {
        Bundle arguments = getArguments();
        type = Objects.requireNonNull(arguments).getInt("type");
        keyword = arguments.getString("keyword");
        orderType = arguments.getInt("orderType");
        timeType = arguments.getInt("timeType");

        switch (type) {
            case 1: // 文章
                return new SearchResultNewsAdapter(keyword);
            case 2: // 快讯
                return new SearchResultFlashNewsAdapter(keyword);
            case 3: // 专题
                return new SearchResultChoiceTopicAdapter(keyword);
            case 4: // 专栏
                return new SearchResultColumnAdapter();
            case 5: // 活动
                return new SearchResultActivityAdapter(keyword);
        }

        return new SearchResultNewsAdapter(keyword);
    }

    @Override
    protected IPaginationPresenter<IPaginationView<SearchResultResponse>, SearchResultResponse> createPresenter() {
        return new SearchMoreListPresenter(keyword, orderType, timeType, type);
    }

    public void reset(){
        initialize(null);
    }

}
